/**
 *====================================================
 * 文件名称: PickupApplicationServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.PickupApplicationEntity;
import com.yuexunit.finance.project.entity.lexicon.ApplicationSimpleStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ApplicationStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ExchangeStatusEnum;
import com.yuexunit.finance.project.entity.lexicon.PickupMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.PickupTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;

/**
 * @ClassName: PickupApplicationServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(PickupApplicationService.class)
public class PickupApplicationServiceImpl implements PickupApplicationService {

	@Override
	public Long createPickupApplicationEntity(PickupApplicationEntity entity) {
		entity.setApplicationState(ApplicationStateEnum.NOT_APPROVE);
		entity.setApplicantDateTime(DateUtil.now());
		entity.setAuditAmount(BigDecimal.ZERO);
		entity.setPickupAmount(BigDecimal.ZERO);
		entity.setDelayAmount(BigDecimal.ZERO);
		entity.setReceivedAmount(BigDecimal.ZERO);
		String numberPre = SettingConstants.PRE_NUMBER_NORMAL_PICKUP;
		if (PickupTypeEnum.AHEAD == entity.getPickupType()) {
			numberPre = SettingConstants.PRE_NUMBER_AHEAD_PICKUP;
		} else if (PickupTypeEnum.REDEEM == entity.getPickupType()) {
			numberPre = SettingConstants.PRE_NUMBER_REDEEM_PICKUP;
		}
		if (entity.getCreditQuotaDeduct() == null) {
			entity.setCreditQuotaDeduct(BigDecimal.ZERO);
		}
		if (entity.getExpense() == null) {
			entity.setExpense(BigDecimal.ZERO);
		}
		if (entity.getExchangeStatus() == null) {
			entity.setExchangeStatus(ExchangeStatusEnum.UNKNOWN);
		}
		entity.setPickupNumber(numberPre + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getPickupId();
	}

	@Override
	public void updatePickupApplication(PickupApplicationEntity entity) {
		PickupApplicationEntity persist = inquirePickupApplication(entity.getPickupId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovatePickupApplication(PickupApplicationEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public PickupApplicationEntity inquirePickupApplication(Long pickupId) {
		return PersistUtil.get(PickupApplicationEntity.class, pickupId);
	}

	@Override
	public PageList<PickupApplicationEntity> inquirePickupApplicationPageList(Long[] pickupIds, Long enterpriseId, Long financeId, String financeNumber, ApplicationSimpleStateEnum applicationState, PickupTypeEnum pickupType, Date beginDate,
			Date endDate, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		if (pickupIds != null && pickupIds.length > 0) {
			criteria.add(Restrictions.in("pickupId", pickupIds));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("applicantDateTime", endDate));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("applicantDateTime", beginDate));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (applicationState != null) {
			criteria.add(Restrictions.eq("applicationState", applicationState));
		}
		if (pickupType != null) {
			criteria.add(Restrictions.eq("pickupType", pickupType));
		}
		criteria.addOrder(Order.desc("applicantDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public PageList<PickupApplicationEntity> inquirePickupApplicationPageLists(List<Long> financeIds, Long[] pickupIds, Long enterpriseId, Long financeId, String financeNumber, ApplicationSimpleStateEnum applicationState, PickupTypeEnum pickupType, Date beginDate,
			Date endDate, String enterpriseName,String bulkstockName,Integer pickupMethod, String pickupNumber, PageCriteria page) {
		List<Long> financeIds2 = null;
		if (bulkstockName !=null && !"".equals(bulkstockName)) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
			if (StringUtils.isNotBlank(bulkstockName)) {
				criteria.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds2 = PersistUtil.get(criteria);
			if (financeIds2.isEmpty()) {
				return new PageList<>(page);
			}
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		if (pickupIds != null && pickupIds.length > 0) {
			criteria.add(Restrictions.in("pickupId", pickupIds));
		}
		if (financeIds2 != null && financeIds2.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds2));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("applicantDateTime", endDate));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("applicantDateTime", beginDate));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (applicationState != null) {
			criteria.add(Restrictions.eq("applicationState", applicationState));
		}
		if (pickupType != null) {
			criteria.add(Restrictions.eq("pickupType", pickupType));
		}
		if (pickupMethod != null && pickupMethod.intValue()>0 ) {
			criteria.add(Restrictions.eq("pickupMethod", PickupMethodEnum.valueOf(pickupMethod)));
		}
		if (StringUtils.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		if (StringUtils.isNotBlank(pickupNumber)) {
			criteria.add(Restrictions.like("pickupNumber", "%" + pickupNumber + "%"));
		}
		if (financeIds == null || financeIds.size() == 0) {
			return new PageList<PickupApplicationEntity>(page);
		}
		if (financeIds != null && financeIds.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		criteria.addOrder(Order.desc("applicantDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<PickupApplicationEntity> inquirePickupApplicationList(Long enterpriseId,
			Set<ApplicationStateEnum> applicationState, PickupMethodEnum pickupMethod, String enterpriseName,
			Date beginDate, Date endDate, Long[] pickupIds) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		if (pickupIds != null && pickupIds.length > 0) {
			criteria.add(Restrictions.in("pickupId", pickupIds));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		
		if (applicationState != null) {
			criteria.add(Restrictions.in("applicationState", applicationState));
		}
		
		if (pickupMethod != null) {
			criteria.add(Restrictions.eq("pickupMethod", pickupMethod));
		}
		
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		
		if (endDate != null) {
			criteria.add(Restrictions.le("applicantDateTime", endDate));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("applicantDateTime", beginDate));
		}
		
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitAuditPickupApplication(List<Long> financeIds) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.or(Restrictions.in("applicationState", new ApplicationStateEnum[] { ApplicationStateEnum.NOT_APPROVE}), Restrictions.and(Restrictions.in("exchangeStatus", new ExchangeStatusEnum[] { ExchangeStatusEnum.ASSESSMENT, ExchangeStatusEnum.SYNCHRONIZATION, ExchangeStatusEnum.CONCLUDE }), Restrictions.or(Restrictions.eq("applicationState", ApplicationStateEnum.AGREE)))));
		criteria.add(Restrictions.eq("pickupType", PickupTypeEnum.NORMAL));
		if (financeIds != null && financeIds.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitReceivablesPickupApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("pickupMethod", PickupMethodEnum.CASH_DELIVERY));
		criteria.add(Restrictions.in("applicationState", new ApplicationStateEnum[] { ApplicationStateEnum.AGREE, ApplicationStateEnum.RECEIPT, ApplicationStateEnum.RELEASE }));
		criteria.add(Restrictions.gtProperty("pickupAmount", "receivedAmount"));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitDeliveryPickupApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.or(Restrictions.eq("applicationState", ApplicationStateEnum.RECEIPT),
				Restrictions.and(Restrictions.eq("exchangeStatus", ExchangeStatusEnum.FINISHED), Restrictions.eq("applicationState", ApplicationStateEnum.RECEIPT))));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitAuditAheadApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("applicationState", ApplicationStateEnum.NOT_APPROVE));
		criteria.add(Restrictions.eq("pickupType", PickupTypeEnum.AHEAD));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitAuditRedeemApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("applicationState", ApplicationStateEnum.NOT_APPROVE));
		criteria.add(Restrictions.eq("pickupType", PickupTypeEnum.REDEEM));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("pickupId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public BigDecimal inquirePickupApplicationWaitCreditQuota(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.add(Restrictions.eq("applicationState", ApplicationStateEnum.NOT_APPROVE));
		criteria.setProjection(Projections.projectionList().add(Projections.sum("creditQuotaDeduct")));
		List<BigDecimal> list = PersistUtil.get(criteria);
		if (list == null || list.isEmpty()) {
			return BigDecimal.ZERO;
		}
		return list.get(0);
	}

	@Override
	public List<PickupApplicationEntity> inquirePickupApplicationCapitalList(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		criteria.add(Restrictions.eq("pickupMethod", PickupMethodEnum.CASH_DELIVERY));
		criteria.add(Restrictions.in("applicationState", new ApplicationStateEnum[] { ApplicationStateEnum.AGREE, ApplicationStateEnum.RECEIPT, ApplicationStateEnum.RELEASE }));
		return PersistUtil.get(criteria);
	}
	
	public List<PickupApplicationEntity> inquirePickupApplicationList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.or(Restrictions.in("applicationState", new ApplicationStateEnum[] { ApplicationStateEnum.NOT_APPROVE}),
				Restrictions.and(Restrictions.in("exchangeStatus", new ExchangeStatusEnum[] { ExchangeStatusEnum.ASSESSMENT, ExchangeStatusEnum.SYNCHRONIZATION, ExchangeStatusEnum.CONCLUDE }), Restrictions.eq("applicationState", ApplicationStateEnum.AGREE))));
		criteria.add(Restrictions.eq("pickupType", PickupTypeEnum.NORMAL));
		criteria.addOrder(Order.desc("applicantDateTime"));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<PickupApplicationEntity> inquirePickupApplicationCompleteList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		criteria.add(Restrictions.eq("applicationState", ApplicationStateEnum.RELEASE));
		criteria.add(Restrictions.eq("pickupType", PickupTypeEnum.NORMAL));
		criteria.addOrder(Order.desc("applicantDateTime"));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Date> inquireWarning(Long[] pickupIds, String colName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationEntity.class);
		if (pickupIds != null && pickupIds.length > 0) {
			criteria.add(Restrictions.in("pickupId", pickupIds));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty(colName)));
		return PersistUtil.get(criteria);
	}
}
